package com.plian.system.service.hr.staff.impl;

import com.plian.Tools.StringUtil;
import com.plian.system.dto.hr.staff.DirectorDTO;
import com.plian.system.entity.hr.staff.Director;
import com.plian.system.mapper.hr.staff.DirectorMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.hr.staff.IDirectorService;
import com.plian.system.vo.hr.staff.DirectorVO;
import com.plian.system.vo.report.HeaderVO;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.vo.report.ReportVO;
import com.plian.system.wrapper.hr.staff.DirectorWrapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 董事会 服务实现类
 *
 * @author gangan
 * @since 2019-11-12
 */
@Service
@AllArgsConstructor
public class DirectorServiceImpl extends BaseServiceImpl<DirectorMapper, Director> implements IDirectorService {

    private DirectorWrapper directorWrapper;

    @Override
    public ReportReturnVO report(DirectorDTO directorDTO) {
        ReportReturnVO reportReturnVO = new ReportReturnVO();
        ReportVO reportVO = new ReportVO();

        reportVO
                .addHeader(HeaderVO.builder().key("companyName").title("单位").build())
                .addHeader(HeaderVO.builder().key("staffName").title("姓名").build())
                .addHeader(HeaderVO.builder().key("isOutDirector").title("是否外派董事").build())
                .addHeader(HeaderVO.builder().key("directorshipName").title("董事会职务").build())
                .addHeader(HeaderVO.builder().key("genderName").title("性别").build())
                .addHeader(HeaderVO.builder().key("birthday").title("出生日期").build())
                .addHeader(HeaderVO.builder().key("appointmentTime").title("任职时间").build());

        List<Director> directors = baseMapper.reportDataPage(directorDTO.getCompanyIds(), directorDTO.getGenderIds(),
                directorDTO.getDirectorshipIds(), directorDTO.getIsOutDirector(), directorDTO.getStaffName());
        List<DirectorVO> list = directorWrapper.entityToVO(directors);


        //给所有数据按照单位编码升序，人员编码升序
        list = list.parallelStream().filter(directorVO -> StringUtil.isNotBlank(directorVO.getCompanyCode()))
                .sorted(Comparator.comparing(DirectorVO::getCompanyCode).thenComparing(DirectorVO::getCreateTime)).collect(Collectors.toList());
        List<DirectorVO> resultData = new ArrayList<>() ;
        String companyNameSearchId = "";
        int count = 0;
        int sum = 0;
        for(int i =0;i< list.size();i++){
            if(!companyNameSearchId.equals(list.get(i).getCompanyId())){
                if(i>0){
                    DirectorVO Total = new DirectorVO();
                    Total.setCompanyName("小计：" + count);
                    resultData.add(Total);
                    sum+=count;
                }
                companyNameSearchId = list.get(i).getCompanyId();
                count = 1;
            }else{
                count++;
            }
            resultData.add(list.get(i));
        }
        sum+=count;
        DirectorVO LastTotal = new DirectorVO();
        LastTotal.setCompanyName("小计：" + count);
        resultData.add(LastTotal);

        DirectorVO SumTotal = new DirectorVO();
        SumTotal.setCompanyName("总计：" + sum);
        resultData.add(SumTotal);


        reportVO.setData(resultData);
        reportReturnVO.setGrids(reportVO);
        return reportReturnVO;
    }
}
